  <template>
      <div class="edit-popup">
          <popup
              ref="popupRef"
              :title="popupTitle"
              :async="true"
              width="950px"
              :clickModalClose="true"
              @confirm="handleSubmit"
              @close="handleClose"
          >
              <el-form
                  ref="formRef"
                  :model="formData"
                  label-width="100px"
                  :rules="formRules"
                  :inline="true"
              >
                  <el-form-item label="客户等级" prop="clientGradeid">
                      <el-select
                          class="flex-1 w-[300px]"
                          v-model="formData.clientGradeid"
                          placeholder="请选择客户等级"
                      >
                          <el-option label="全部" value="" />
                          <el-option label="一般客户" value="1" />
                          <el-option label="重要客户" value="2" />
                          <el-option label="潜在客户" value="3" />
                          <el-option label="个人" value="4" />
                          <el-option label="企业" value="5" />
                          <el-option label="中介" value="6" />
                          <el-option label="中型客户" value="7" />
                          <el-option label="人型客户" value="8" />
                          <el-option label="中型客户" value="9" />
                          <el-option label="小型客户" value="10" />
                          <el-option label="一级-新" value="11" />
                          <el-option label="一级-老" value="12" />
                          <el-option label="二级-新" value="13" />
                          <el-option label="二级-老" value="14" />
                          <el-option label="三级-新" value="15" />
                          <el-option label="三级-老" value="16" />
                      </el-select>
                  </el-form-item>
                  <el-form-item label="转入公海时间" prop="openSeaTime">
                      <el-date-picker
                          style="width: 300px"
                          class="flex-1 !flex"
                          v-model="formData.openSeaTime"
                          type="datetime"
                          clearable
                          value-format="YYYY-MM-DD hh:mm:ss"
                          placeholder="请选择转入公海时间"
                      />
                  </el-form-item>
                  <el-form-item label="前业务人员" prop="oldBusinessStaffid">
                      <el-select
                          class="flex-1 w-[300px]"
                          v-model="formData.oldBusinessStaffid"
                          placeholder="请选择前业务人员"
                      >
                          <el-option label="张三" value="1" />
                          <el-option label="李四" value="2" />
                          <el-option label="王五" value="3" />
                      </el-select>
                  </el-form-item>
                  <el-form-item label="客户名称" prop="clientName">
                      <el-input class="w-[300px]" v-model="formData.clientName" placeholder="请输入客户名称" />
                  </el-form-item>
                  <el-form-item label="客户责任人" prop="clientWho">
                      <el-input class="w-[300px]" v-model="formData.clientWho" placeholder="请输入客户责任人" />
                  </el-form-item>
                  <el-form-item label="联系电话" prop="phone">
                      <el-input class="w-[300px]" v-model="formData.phone" placeholder="请输入联系电话" />
                  </el-form-item>
                  <el-form-item label="邮件" prop="mail">
                      <el-input class="w-[300px]" v-model="formData.mail" placeholder="请输入邮件" />
                  </el-form-item>
                  <el-form-item label="公海池" prop="openSeaPool">
                      <el-input class="w-[300px]" v-model="formData.openSeaPool" placeholder="请输入公海池" />
                  </el-form-item>
              </el-form>
          </popup>
      </div>
  </template>
  <script lang="ts" setup>
  import type { FormInstance } from 'element-plus'
  import { highseasEdit, highseasAdd, highseasDetail } from '@/api/highseas'
  import Popup from '@/components/popup/index.vue'
  import feedback from '@/utils/feedback'
  import type { PropType } from 'vue'
  defineProps({
      dictData: {
          type: Object as PropType<Record<string, any[]>>,
          default: () => ({})
      }
  })
  const emit = defineEmits(['success', 'close'])
  const formRef = shallowRef<FormInstance>()
  const popupRef = shallowRef<InstanceType<typeof Popup>>()
  const mode = ref('add')
  const popupTitle = computed(() => {
      return mode.value == 'edit' ? '编辑客户公海' : '新增客户公海'
  })

  const formData = reactive({
      id: '',
      clientGradeid: '',
      openSeaTime: '',
      oldBusinessStaffid: '',
      clientName: '',
      clientWho: '',
      phone: '',
      mail: '',
      openSeaPool: ''
  })

  const formRules = {}

  const handleSubmit = async () => {
      await formRef.value?.validate()
      const data: any = { ...formData }
      mode.value == 'edit' ? await highseasEdit(data) : await highseasAdd(data)
      popupRef.value?.close()
      feedback.msgSuccess('操作成功')
      emit('success')
  }

  const open = (type = 'add') => {
      mode.value = type
      popupRef.value?.open()
  }

  const setFormData = async (data: Record<string, any>) => {
      for (const key in formData) {
          if (data[key] != null && data[key] != undefined) {
              //@ts-ignore
              formData[key] = data[key]
          }
      }
  }

  const getDetail = async (row: Record<string, any>) => {
      const data = await highseasDetail({
          id: row.id
      })
      setFormData(data)
  }

  const handleClose = () => {
      emit('close')
  }

  defineExpose({
      open,
      setFormData,
      getDetail
  })
  </script>
  <style scoped>
  ::v-deep .el-collapse-item__header {
    padding-left: 54px;
    background-color: var(--el-bg-color-collapse);
    font-size: 16px;
    font-weight: 600;
    color: var(--el-text-color-collapse);
  }
  ::v-deep .el-collapse-item__wrap {
      padding-top: 40px;
  }
  </style>
